#ifdef _WIN32 
#include <windows.h> 
#endif

#include "Gatherer.h"

Gatherer::Gatherer(glm::vec3 _Pos, float _r, float _speed, std::string _Name,
		std::string _modelFName, ModelPool *_mPool, PathFinder *_pathfinder,
		Terrain::Terrain *_ter) :
		Villager(_Pos, _r, _speed, _Name, _modelFName, _mPool, _pathfinder, _ter)
{
	type = OBJ_GATHERER;
	parentType = OBJ_VILLAGER;

	for (int i = 0; i < 33; i++)
	{
		std::stringstream ss; //create a stringstream
		ss << i + 1; //add number to the stream
		modelID.push_back(mPool->loadModel(modelFName, "_a_00000" + ss.str()));
	}

	costGold = 100;
	status = NOTHING;
}

Gatherer::~Gatherer(void)
{
}

void Gatherer::control(float t)
{
	// exit when unit is sleeping
	if (sleeping)
		return;

	if (MoveVector.size() > 0)
	{

		move();
	}
	else
	{
		Moving = false;
	}

	doWood();

	updateAnimation();
	visible = World::world->SphereInFrustum(Pos, 1.0);
}

void Gatherer::goTo(glm::vec2 pos)
{

}

//Gathering ai functions. Simple finite state machine. All transitions between states are
//are based on simple conditions

void Gatherer::setWork(glm::vec2 sTreePosition, glm::vec2 sGatherPosition)
{
	TreePosition = sTreePosition;
	GatherPosition = sGatherPosition;
	status = READY;
}

void Gatherer::doWood()
{

	if (status == NOTHING)
	{
		return;
	}

	else if (status == READY)
	{

		MoveVector = pathfinder->getPath(TreePosition, this->get2dPosition());
		resFlag = true;
		newMove = true;

		status = TO_TREE;

	}
	//If gatherer reaches the resource, send him back(simulating he already harvested needed things)
	else if (status == TO_TREE)
	{
		if (MoveVector.size() < 1)
		{
			status = DONE;
		}
	}
	//If he is done, that means send him back with resources
	else if (status == DONE)
	{

		MoveVector = pathfinder->getPath(GatherPosition, this->get2dPosition());
		resFlag = true;
		newMove = true;
		status = FROM_TREE;

	}
	//If gatherer reaches it's building, send him harvest again
	else if (status == FROM_TREE)
	{
		if (MoveVector.size() < 1)
			status = READY;
	}

}

bool Gatherer::doCut()
{
	return Lumbertime.GetInterval(2000);
}

void Gatherer::doThink()
{

}

int Gatherer::getStatus()
{
	return status;
}
